/*
 * Copyright 2010 Electronic Business Systems Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gr.ebs.gssdroid.widget;

import gr.ebs.gssdroid.R;
import gr.ebs.gssdroid.resource.GroupResource;
import gr.ebs.gssdroid.resource.GroupUserResource;
import gr.ebs.gssdroid.resource.GroupsResource;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

public class GroupsListView extends ExpandableListView{
	LayoutInflater inflater;
	GroupsListAdapter adapter;
	public GroupsListView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public GroupsListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}
	
	public GroupsListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}
	public void setGroup(GroupsResource res){
		setAdapter(adapter=new GroupsListAdapter(res));
	}
	
	public GroupsResource getGroup(){
		return adapter.groupsResource;
	}
	
	public class GroupsListAdapter extends BaseExpandableListAdapter{
		GroupsResource groupsResource;
		public GroupsListAdapter(GroupsResource groups) {
			this.groupsResource = groups;
		}
		public void setGroupsResource(GroupsResource groupsResource) {
			this.groupsResource = groupsResource;
		}
		
		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			View v = inflater.inflate(R.layout.groupsrow, null);
			ImageView imageIcon = (ImageView) v.findViewById(R.id.image);
			
			TextView textFileName = (TextView) v.findViewById(R.id.name);
			GroupUserResource gr = (GroupUserResource) getChild(groupPosition, childPosition);
			textFileName.setText(gr.getName());
			imageIcon.setImageResource(R.drawable.user);
			
			return v;
		}
		
		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			View v = inflater.inflate(R.layout.groupsrow, null);
			ImageView imageIcon = (ImageView) v.findViewById(R.id.image);
			TextView textFileName = (TextView) v.findViewById(R.id.name);
			GroupResource gr = (GroupResource) getGroup(groupPosition);
			textFileName.setText(gr.getName());
			imageIcon.setImageResource(R.drawable.group);
			//imageIcon.setVisibility(View.GONE);
			return v;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			if(groupsResource.getGroups().size()>groupPosition)
				return groupsResource.getGroups().get(groupPosition).getUsers().size();
			return 0;
		}

		@Override
		public Object getGroup(int groupPosition) {
			if(groupsResource.getGroups().size()>groupPosition)
				return groupsResource.getGroups().get(groupPosition);
			return null;
		}

		@Override
		public int getGroupCount() {
			return groupsResource.getGroups().size();
		}
		
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			if(groupsResource.getGroups().size()>groupPosition)
				if(groupsResource.getGroups().get(groupPosition).getUsers().size()>childPosition)
					return groupsResource.getGroups().get(groupPosition).getUsers().get(childPosition);
			return null;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}
		
		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}
		
		

		

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return false;
		}
		
	}
	public GroupsListAdapter getListdapter() {
		return adapter;
	}

}
